var member = {

    column : [{key:"phone",name:"手机号码"},{key:"name",name:"姓名"}], //显示字段
    path : null, //请求地址
    columnNum : null, //column的总显示列数
    inputChecked : ["crmMemberLevel","crmBalance","storeName","marketName","lastCostTime","crmScore"], //默认显示字段

    //绘制模态框
    drawMemberShowColumn : function(path_){
        member.path = path_;

        if ($("#myModal").find('.checkbox').length > 0) {
            return;
        }
        var html_ = "";
        html_ += '<div class="modal-dialog">'
        html_ += '<div class="modal-content">'
        html_ += '<div class="modal-header" style="background-color: #CCCCCC" >'
        html_ += '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>'
        html_ += '<h4 class="modal-title" id="myModalLabel">数据显示</h4></div>'
        html_ += '<div class="modal-body">'
        html_ += '<div>'
            /*<p class="text-justify">

            </p>*/
        html_ += '</div>'
        html_ += '为了方便您识别会员，手机、会员名称固定显示在列表中不可取消勾选；除手机、会员名称外，您可在下列字段中最多再勾选6项'
        html_ += '达到勾选项上限后其他选项均为不可勾选状态，须取消已勾选项空出限额后才可继续勾选'

        html_ += '<div class="checkbox">'
        html_ += '<div class="text-left">会员基本信息</div>'
        html_ += '<div>'
            //<%--<label><input id="input-checkbox" onclick="is_checked(this)" type="checkbox" value="1">会员卡号</label>--%>
        html_ += '<label><input id="input-checkbox" onclick="return false" type="checkbox" checked="checked" value="phone">手机号码</label>'
        html_ += '<label><input id="input-checkbox" onclick="return false" type="checkbox" checked="checked" value="name">会员姓名</label>'
        html_ += '<label><input id="input-checkbox" onclick="member.is_checked(this)" type="checkbox" value="wechat">微信号</label>'
        html_ += '<label><input id="input-checkbox" onclick="member.is_checked(this)" type="checkbox" value="weibo">微博号</label>'
        html_ += '<label><input id="input-checkbox" onclick="member.is_checked(this)" type="checkbox" value="qq">QQ号</label>'
        html_ += '<label><input id="input-checkbox" onclick="member.is_checked(this)" type="checkbox" value="email">邮箱</label>'
        html_ += '<label><input id="input-checkbox" onclick="member.is_checked(this)" type="checkbox" value="address">居住地址</label>'
        html_ += '<label><input id="input-checkbox" onclick="member.is_checked(this)" type="checkbox" value="officeAddress">办公地址</label>'
        html_ += '<label><input id="input-checkbox" onclick="member.is_checked(this)" type="checkbox" value="familySize">家庭人数</label>'
        html_ += '<label><input id="input-checkbox" onclick="member.is_checked(this)" type="checkbox" value="gender">性别</label>'
        html_ += '<label><input id="input-checkbox" onclick="member.is_checked(this)" type="checkbox" value="idCard">身份证</label>'
        html_ += '<label><input id="input-checkbox" onclick="member.is_checked(this)" type="checkbox" value="incomeRange">收入区间</label>'
        html_ += '<label><input id="input-checkbox" onclick="member.is_checked(this)" type="checkbox" value="birthday">生日</label>'
        html_ += '<label><input id="input-checkbox" onclick="member.is_checked(this)" type="checkbox" value="education">学历</label>'
        html_ += '<label><input id="input-checkbox" onclick="member.is_checked(this)" type="checkbox" value="createTime">注册时间</label>'
        html_ += '</div>'
        html_ += '<div class="text-left">会员等级</div>'
        html_ += '<div>'
        html_ += '<label><input id="input-checkbox" onclick="member.is_checked(this)" type="checkbox" value="crmScore">现有积分</label>'
        html_ += '<label><input id="input-checkbox" onclick="member.is_checked(this)" type="checkbox" value="crmMemberLevel">会员等级</label>'
        html_ += '<label><input id="input-checkbox" onclick="member.is_checked(this)" type="checkbox" value="crmBalance">会员储值</label>'
        html_ += '</div>'
        html_ += '<div class="text-left">会员来源</div>'
        html_ += '<div>'
        html_ += '<label><input id="input-checkbox" onclick="member.is_checked(this)" type="checkbox" value="source">渠道来源</label>'
        html_ += '<label><input id="input-checkbox" onclick="member.is_checked(this)" type="checkbox" value="marketName">所属销售</label>'
        html_ += '<label><input id="input-checkbox" onclick="member.is_checked(this)" type="checkbox" value="storeName">所属门店</label>'
        html_ += '</div>'
        html_ += '<div class="text-left">RFM信息</div>'
        html_ += '<div>'
        html_ += '<label><input id="input-checkbox" onclick="member.is_checked(this)" type="checkbox" value="lastCostTime">最后次购买时间</label>'
        html_ += '<label><input id="input-checkbox" onclick="member.is_checked(this)" type="checkbox" value="lastCost">最后次购买金额</label>'
        html_ += '<label><input id="input-checkbox" onclick="member.is_checked(this)" type="checkbox" value="firstCostTime">首次购买时间</label>'
        html_ += '<label><input id="input-checkbox" onclick="member.is_checked(this)" type="checkbox" value="firstCost">首次购买金额</label>'
        html_ += '<label><input id="input-checkbox" onclick="member.is_checked(this)" type="checkbox" value="costCount">购买次数</label>'
        html_ += '<label><input id="input-checkbox" onclick="member.is_checked(this)" type="checkbox" value="costTotal">购买总额</label>'
        html_ += '<label><input id="input-checkbox" onclick="member.is_checked(this)" type="checkbox" value="goodsTotal">购买总件数</label>'
        html_ += '<label><input id="input-checkbox" onclick="member.is_checked(this)" type="checkbox" value="maxCost">最大单笔购买金额</label>'
        html_ += '<label><input id="input-checkbox" onclick="member.is_checked(this)" type="checkbox" value="averageGoodsQuantity">平均购买件数</label>'
        html_ += '<label><input id="input-checkbox" onclick="member.is_checked(this)" type="checkbox" value="averageCost">平均消费</label></div></div></div>'
        html_ += '<div class="modal-footer">'
        html_ += '<button type="button" class="btn btn-primary" onclick="member.save_column()" style="background-color: #5c8ad7">确定</button>'
        html_ += '<button type="button" class="btn btn-primary" data-dismiss="modal" style="background-color: #5c8ad7">关闭</button></div></div></div>'
        $("#myModal").append(html_);
        member.input_checked(path_);
    },

    //显示勾选字段
    input_checked : function(path_){

        var arr = member.get_member_property(path_);
        if (arr != null && arr.length > 0) {
            arr.pop();
            member.column = arr;
            for (var i in arr) {
                $('input[value="' + arr[i].key + '"]').attr("checked", true);
            }
        } else {
            for (var i in member.inputChecked) {
                var o = {};
                $('input[value="' + member.inputChecked[i] + '"]').attr("checked", true);
                o.key = $('input[value="' + member.inputChecked[i] + '"]').val();
                o.name = document.querySelector('input[value="' + member.inputChecked[i] + '"]').nextSibling.textContent;
                member.column.push(o);
            }
        }
    },

    //勾选会员列表展示字段
    is_checked : function(obj){

        if ($("input[type='checkbox']:checkbox:checked").length > 8) {
            obj.checked = false;
            alert("最多只能选中8个");
            return ;
        }
        if (obj.checked){
                var o = {};
                if(member.column.length > 8){
                    alert("最多只能选中8个");
                    obj.checked = false;
                    return ;
                }
            o.key = obj.value;
            o.name = obj.nextSibling.textContent;
               if($.inArray(o,member.column) < 0){
                   member.column.push(o);
               }else{
                   alert("该字段已选中")
                   return;
               }
            }
        else{
            for (var i in member.column){
                if (member.column[i].key == obj.value){
                    member.column.splice(i,1);
                }
            }
        }
    },

    //查询会员列表显示字段
    get_member_property : function(path_){

        var url = path_ + "/member/ajaxGetMemberPropertySetting";

        var e = JSON.parse(member.syncAjax("post", url , null  , false));
        var jsonArray = [];
        var html_ = '';
        if(e.status == '200'){
            var arr = JSON.parse(e.data);
            return arr;
        }
    },

    //保存会员显示字段
    save_column : function() {

        var url = member.path + "/member/ajaxSaveMemberShowColumn";

        for (var i in member.column){
            if (member.column[i].key == "id"){
                member.column.splice(i,1);
            }
        }
        member.column.push({key:"id",name:"id"});
        var data = {
            setting : JSON.stringify(member.column)
        };
        var e = JSON.parse(member.syncAjax("post", url , data  , false));
        if(e.status == '200'){
            window.location.reload();
        }else{
        }
    },

    //data数据处理
    dealData : function(obj){

        var keys = Object.keys(obj);
        var result = "";
        for (var i in keys){
            var column = keys[i];
            var data = obj.column;
            if ("createTime" == column){ //注册时间
                var temp = new Date(data);
                result = temp.getFullYear()+"-"+(temp.getMonth()+1)+"-"+temp.getDate();
            }else if ("name" == column){ //姓名
                if (data.length > 4){
                    result = "<span title='" + data + "'>" + data.substr(0, 3) + "...</span>";
                } else {
                    result = data;
                }
            } else if (column == "wechat"){ //微信
                if (data.length > 5){
                    result = "<span title='" + data + "'>" + data.substr(0, 4) + "...</span>";
                }else{
                    result = data;
                }
            } else if (column == "weibo"){ //微博
                if (data.length > 5){
                    result = "<span title='" + data + "'>" + data.substr(0, 4) + "...</span>";
                }else{
                    result = data;
                }
            } else if (column == "qq"){ //QQ
                if (data.length > 12){
                    result = "<span title='" + data + "'>" + data.substr(0, 11) + "...</span>";
                }else{
                    result = data;
                }
            } else if (column == "email"){ //邮箱
                if (data.length > 12){
                    result = "<span title='" + data + "'>" + data.substr(0, 11) + "...</span>";
                }else{
                    result = data;
                }
            } else if (column == "address"){ //居住地址
                if (data.length > 20){
                    result = "<span title='" + data + "'>" + data.substr(0, 19) + "...</span>";
                }else{
                    result = data;
                }
            } else if (column == "officeAddress"){ //办公地址
                if (data.length > 20){
                    result = "<span title='" + data + "'>" + data.substr(0, 19) + "...</span>";
                }else{
                    result = data;
                }
            } else if (column == "gender") { //性别
                if (data == "m"){
                    result = "男";
                } else if (data == "f"){
                    result = "女";
                } else {
                    result = "未获取";
                }
            } else if (column == "idCard"){ //身份证
                if (data.length > 12){
                    result = "<span title='" + data + "'>" + data.substr(0, 11) + "...</span>";
                }else{
                    result = data;
                }
            } else if (column == "incomeRange"){ //收入区间
                if (data.length > 12){
                    result = "<span title='" + data + "'>" + data.substr(0, 11) + "...</span>";
                }else{
                    result = data;
                }
            } else if (column == "birthday"){ //生日
                var temp = new Date(data);
                result = temp.getFullYear()+"-"+(temp.getMonth()+1)+"-"+temp.getDate();
            } else if (column == "education"){ //学历
                if (data.length > 6){
                    result = "<span title='" + data + "'>" + data.substr(0, 5) + "...</span>";
                }else{
                    result = data;
                }
            } else if (column == "crmScore") { //现有积分
                if (data == null || data == ""){
                    result = "0";
                }else{
                    result = parseInt(data)/100;
                }
            } else if (column == "crmMemberLevel"){ //会员等级
                if (data.length > 6){
                    result = "<span title='" + data + "'>" + data.substr(0, 5) + "...</span>";
                }else{
                    result = data;
                }
            } else if (column == "source"){ //渠道来源
                if (data.length > 6){
                    result = "<span title='" + data + "'>" + data.substr(0, 5) + "...</span>";
                }else{
                    result = data;
                }
            } else if (column == "job_number"){ //客户代表
                if (data.length > 4){
                    result = "<span title='" + data + "'>" + data.substr(0, 3) + "...</span>";
                }else{
                    result = data;
                }
            } else if (column == "lastCost") { //最后购买金额
                if (data == null || data == ""){
                    result = "0";
                }else{
                    result = parseInt(data)/100;
                }
            } else if (column == "firstCost") { //首次购买金额
                if (data == null || data == ""){
                    result = "0";
                }else{
                    result = parseInt(data)/100;
                }
            } else if (column == "maxCost") { //最大单笔购买金额
                if (data == null || data == ""){
                    result = "0";
                }else{
                    result = parseInt(data)/100;
                }
            } else if (column == "lastCostTime"){ //最后购买时间
                if (data == null || data == ""){
                    result = "——";
                }else{
                    var temp = new Date(data);
                    result = temp.getFullYear()+"-"+(temp.getMonth()+1)+"-"+temp.getDate();
                }
            } else if (column == "firstCostTime"){ //首次购买时间
                if (data == null || data == ""){
                    result = "——";
                }else{
                    var temp = new Date(data);
                    result = temp.getFullYear()+"-"+(temp.getMonth()+1)+"-"+temp.getDate();
                }
            } else {
                if (data == null || data == ""){
                    result == "——";
                }else{
                    result = data;
                }
            }
        }
        return obj;
    },

    /**
     * 发送ajax请求
     *
     * type_ : post or get
     * url_ : url
     * data_ : data_
     * async_ : sync type ,true or false
     */
    syncAjax : function(type_, url_, data_  , async_) {
        var msg_ = null;
        $.ajax({
            dataType : "text",
            type : type_,
            url : url_,
            data : data_,
            async : async_,
            success : function(msg) {
                msg_ = msg;
            },
            error : function(msg) {
                msg_ = msg;
            }
        });
        return msg_;
    }
}